<template>
<div>
    <div>
        <div class="search-contain">
          <input v-model="keyword" class="search-input" type="text" placeholder="请输入城市名称">
        </div>
    </div>
    <div class="search-container" v-show="keyword">
      <ul>
        <li class="search-item" v-for="item of list" :key="item.key">{{item.name}}</li>
      </ul>
    </div>
</div>
</template>

<script>
export default {
  name: 'Search',
  props: {
    citys: Object
  },
  data () {
    return {
      keyword: '',
      list: [],
      timer: null
    }
  },
  watch: {
    keyword () {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        const result = []
        for (let i in this.citys) {
          this.citys[i].forEach((value) => {
            if (value.spell.indexOf(this.keyword) > -1 ||
               value.name.indexOf(this.keyword) > -1) {
              result.push(value)
            }
          })
        }
        this.list = result
      }, 100)
    }
  }
}
</script>

<style lang='stylus' scoped>
 @import '~styles/varible.styl';
.search-contain
  text-align :center
  height 0.8rem
  background :#a1afc9
  opacity :0.8
  .search-input
    height :0.6rem
    width :80%
    background :white
    line-height :.6rem
    text-align :center
    border-radius :.15rem
    font-weight :600
    margin:0.1rem 0 0.1rem 0
.search-container
  z-index 1
  overflow hidden
  position absolute
  top 1.58rem
  left 0
  right  0
  bottom 0
  background  #eee
  .search-item
    color #666
    padding-left  .3rem
    background #fff
    border-bottom .05rem solid #eee
    line-height .62rem
</style>
